import API from "@/api/API";
import { BufferGeometry, DoubleSide, Float32BufferAttribute, FrontSide, MeshPhongMaterial, MeshPhongMaterialParameters } from "three";

export interface IModelVI {
    geo:BufferGeometry;
    mat:MeshPhongMaterial;
}

export default class BaseUtils {

    static createModelData(vertices: number[], indices: number[], uvs:number[], color: any, vecColors?: number[]):IModelVI {

        let geo = new BufferGeometry();

        geo.setIndex(indices);
        geo.setAttribute('position', new Float32BufferAttribute(vertices, 3));
        geo.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );

         // 材质参数
         let matparams: MeshPhongMaterialParameters = {
            // color: color,
            side: DoubleSide,
            map:API.pic
        }

        // 是否渲染顶点颜色
        if (vecColors && vecColors.length == vertices.length) {
            geo.setAttribute('color', new Float32BufferAttribute(vecColors, 3));
            matparams = {
                side: DoubleSide,
                vertexColors: true,
            }
        }
        // 计算法线
        geo.computeVertexNormals();

        // 材质
        let mat = new MeshPhongMaterial(matparams);

        return {geo, mat};
    }

}